package com.robot.admin.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.robot.admin.client.OrdersClient;
import com.robot.admin.dao.vo.AgentDao;
import com.robot.admin.pojo.client.Settlement;
import com.robot.admin.pojo.client.SettlementDetails;
import com.robot.admin.pojo.vo.CashWithdrawal;
import com.robot.admin.util.CastEntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author lin
 */
@Configuration
@EnableScheduling
public class AgentJob {
    @Autowired
    private AgentDao agentDao;
    @Autowired
    private OrdersClient ordersClient;

    /**
     * 每天中午12点触发
     *
     * @throws Exception
     */
//    @Scheduled(cron = "0 0 12 * * ?")
//    private void configureTasks() throws Exception {
//        List<CashWithdrawal> cashWithdrawals = CastEntityUtil.castEntity(agentDao.findYesterdayCashWithdrawals(), CashWithdrawal.class);
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        for (CashWithdrawal cashWithdrawal : cashWithdrawals) {
//            Settlement settlement = new Settlement();
//            settlement.setAgentId(cashWithdrawal.getAgentId());
//            settlement.setAmount(cashWithdrawal.getTotalPayAmount());
////           LocalDateTime date = simpleDateFormat.parse(cashWithdrawal.getDate());
//            LocalDate date=(LocalDate) cashWithdrawal.getDate();
//            settlement.setSettlementDate(date);
//            settlement.setStatus("0");
//            LocalDateTime now = LocalDateTime.now();
//            settlement.setCreatedAt(now);
//            settlement.setUpdatedAt(now);
//
//            JSONArray objects = JSON.parseArray(ordersClient.thirdFindSubOrdersByAgentIdAndCreatedAt(cashWithdrawal.getAgentId(), cashWithdrawal.getDate(), 1, 1));
//            for (int i = 0; i < objects.size(); i++) {
//                JSONObject jsonObject = objects.getJSONObject(i);
//
//                SettlementDetails settlementDetails = new SettlementDetails();
//                settlementDetails.setAgentId(cashWithdrawal.getAgentId());
//                settlementDetails.setMemberId((String) jsonObject.get("memberId"));
//                settlementDetails.setCompanyId((String) jsonObject.get("companyId"));
//                settlementDetails.setStoreId((String) jsonObject.get("storeId"));
//                settlementDetails.setMemberGrade((String) jsonObject.get("memberGrade"));
//                settlementDetails.setPrice(new BigDecimal(jsonObject.get("price").toString()));
//                settlementDetails.setRate(Double.parseDouble(jsonObject.get("rate").toString()));
//                settlementDetails.setDiscountAmount(new BigDecimal(jsonObject.get("discountAmount").toString()));
//                settlementDetails.setPayAmount(new BigDecimal(jsonObject.get("payAmount").toString()));
//                settlementDetails.setTotalRevenue(new BigDecimal(jsonObject.get("totalRevenue").toString()));
//                settlementDetails.setAgentRevenue(new BigDecimal(jsonObject.get("agentRevenue").toString()));
//                settlementDetails.setRevenue(new BigDecimal(jsonObject.get("revenue").toString()));
//                settlementDetails.setPrincipal(new BigDecimal(jsonObject.get("principal").toString()));
//                settlementDetails.setRoyalty(new BigDecimal(jsonObject.get("royalty").toString()));
//                settlementDetails.setCreatedAt(now);
//                settlementDetails.setUpdatedAt(now);
//                ordersClient.thirdAddSettlementDetails(JSON.toJSONString(settlementDetails));
//            }
//            ordersClient.thirdAddSettlement(JSON.toJSONString(settlement));
//        }
//    }
}
